package com.dream.inter.action;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dream.common.JsonString;
import com.dream.common.Msg;
import com.dream.common.SuperController;
import com.dream.model.SysUser;
import com.dream.model.Sys_Class;
import com.dream.model.Sys_Room;
import com.dream.model.Sys_TestPaper;
import com.dream.system.service.ISysClass;
import com.dream.system.service.ISysClassPlant;
import com.dream.system.service.ISysLable;
import com.dream.system.service.ISysUser;
import com.dream.system.service.ISys_TestPaper;
import com.dream.system.service.TellValidateService;

@SuppressWarnings("all")
@Controller
@RequestMapping({ "/inter/loginAction1.do" })
public class LoginActionInter1 extends SuperController {

	@Autowired
	private ISysClassPlant sysClassPlantImpl;
	@Autowired
	private ISysUser sysUserImpl;
	@Autowired
	private TellValidateService tellValidateServiceImpl;

	@Autowired
	private ISys_TestPaper sys_TestPaperServiceImpl;

	@Autowired
	private ISysLable sysLableService;

	@Autowired
	private ISysClass sysClassServiceImpl;

	/**
	 * 
	 * 按班级密码登录，返回班级
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=getzhucema1" })
	public void getzhucema1(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String mima = request.getParameter("bajima");
		Sys_Class sc = sysClassServiceImpl.getzhucema(mima);
		String jsonpcallback = request.getParameter("jsonpcallback");
		if (sc == null) {
			response.getWriter().write(jsonpcallback + "({code:1})");
			response.getWriter().flush();
			response.getWriter().close();

		} else {
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(JsonString.sys_class);
			response.getWriter().write(jsonpcallback + "(" + JSONObject.fromObject(sc, jc).toString() + ")");
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 
	 * 按班级密码登录，返回班级
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=getzhucema" })
	public void getzhucema(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String mima = request.getParameter("bajima");
		Sys_Class sc = sysClassServiceImpl.getzhucema(mima);
		// String jsonpcallback =request.getParameter("jsonpcallback");
		if (sc == null) {
			response.getWriter().write("-1");
			response.getWriter().flush();
			response.getWriter().close();

		} else {
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(JsonString.sys_class);
			response.getWriter().write(JSONObject.fromObject(sc, jc).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 增加用户
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=addUser")
	public void addUser(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String classId = request.getParameter("classId");
		if (null == classId || "".equals(classId)) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		model.setUserEngListName(model.getUserName());
		this.sysUserImpl.addUser1(model);
		List<Sys_Class> sc = this.sysClassServiceImpl.getSysClassById(classId);
		if (model == null || model.getTable_id() == null) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		this.sysClassServiceImpl.addTeacherAddClass(sc, model.getTable_id());
		if (model != null) {
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(JsonString.sys_user);
			response.getWriter().write(JSONObject.fromObject(model, jc).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 这里只是学生所有上过的班
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getStudentClass")
	public void getStudentClass(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String table_id = request.getParameter("user_table_id");
		if (null == table_id || "".equals(table_id)) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		SysUser sysUser = new SysUser();
		sysUser.setTable_id(table_id);
		Map map1 = this.sysUserImpl.getStudentInfo(sysUser, "1");
		if (map1.get("list") == null) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		List<SysUser> listsulaoshi = (List<SysUser>) map1.get("list");

		Map map = this.sysUserImpl.getStudentClass(sysUser);

		if (map.get("list") != null) {
			List<Sys_Class> list = (List<Sys_Class>) map.get("list");
			for (int i = 0; i < list.size(); i++) {
				Sys_Class sc = list.get(i);
				String class_table_id = sc.getTable_id();
				String year = sc.getString1();
				List<SysUser> listsu = this.sysUserImpl.getClassTeacherCurrrt(sc);
				if (listsu != null && listsu.size() > 0) {
					list.get(i).setString2(listsu.get(0).getTable_id());
					list.get(i).setString3(listsu.get(0).getUserName());
					list.get(i).setString4(listsu.get(0).getUserImage());
				}
			}
		}
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 得到学生当前老师的，在同一班的教室
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getStudent_Teacher_Room")
	public void getStudent_Teacher_Room(SysUser teacher1, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String table_id = request.getParameter("user_table_id");
		String teacher_table_id = request.getParameter("teacher_table_id");

		if (table_id == null || "".equals(table_id) || teacher_table_id == null || "".equals(teacher_table_id)) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		SysUser sysUser = new SysUser();
		sysUser.setTable_id(table_id);
		SysUser teacher = new SysUser();
		teacher.setTable_id(teacher_table_id);
		String sys_class_table_id = request.getParameter("sys_Class_table_id");
		String year = request.getParameter("year");
		Map sr = this.sysClassServiceImpl.getStudent_Teacher_Room(sysUser, teacher, sys_class_table_id, year);
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(JsonString.sys_room);
		JSONObject jonArray = JSONObject.fromObject(sr, jc);
		response.getWriter().write(jonArray.toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 三方登录
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=loginInfo")
	public void loginInfo(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser mapu = this.sysUserImpl.getUserInfo(model);
		if (mapu == null) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		String table_id = mapu.getTable_id();
		if (null == table_id || "".equals(table_id)) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		SysUser sysUser = new SysUser();
		sysUser.setTable_id(table_id);
		Map map1 = this.sysUserImpl.getStudentInfo(sysUser, "1");
		if (map1.get("list") == null) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		List<SysUser> listsulaoshi = (List<SysUser>) map1.get("list");
		Map map = this.sysUserImpl.getStudentClass(sysUser);
		if (map.get("list") != null) {
			List<Sys_Class> list = (List<Sys_Class>) map.get("list");
			for (int i = 0; i < list.size(); i++) {
				Sys_Class sc = list.get(i);
				String class_table_id = sc.getTable_id();
				String year = sc.getString1();
				List<SysUser> listsu = this.sysUserImpl.getClassTeacherCurrrt(sc);
				if (listsu != null && listsu.size() > 0) {
					list.get(i).setString2(listsu.get(0).getTable_id());
					list.get(i).setString3(listsu.get(0).getUserName());
					list.get(i).setString4(listsu.get(0).getUserImage());
				}
			}
		}
		map.put("sysUser", mapu);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 占位置
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addStudentRoomOption")
	public void addStudentRoomOption(Sys_Room model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String user_table_id = request.getParameter("user_table_id");
		if (null == user_table_id || "".equals(user_table_id)) {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		SysUser sysUser = new SysUser();
		sysUser.setTable_id(user_table_id);
		if (null == sysUser) {
			System.out.println("----------------没有用户-----------------------");
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
		int map = this.sysClassServiceImpl.addStudentRoomOption(model, sysUser);
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "对不起，此位置已经有人抢先一步了，请另选", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 查询用户的标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getUserLable")
	public void getUserLable(Sys_Room model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String user_table_id = request.getParameter("user_table_id");
		if (null == user_table_id || "".equals(user_table_id)) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		JSONArray json = this.sysUserImpl.getUserLable(user_table_id);
		response.getWriter().write(json.toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询一个学生，现在所有的作业juan。
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getHomeWorkComplet")
	public void getHomeWorkComplet(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String user_table_id = request.getParameter("user_table_id");
		if (null == user_table_id || "".equals(user_table_id)) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		SysUser sysUser = new SysUser();
		sysUser.setTable_id(user_table_id);
		if (sysUser == null) {
			return;
		}
		Map map = this.sys_TestPaperServiceImpl.getSudentTestPaper(model, sysUser);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.test_Paper);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询一个学生，现在所有的卷的作业。
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTest_PaperQuestionInfo")
	public void getTest_PaperQuestionInfo(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String user_table_id = request.getParameter("user_table_id");
		if (null == user_table_id || "".equals(user_table_id)) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		SysUser sysUser = new SysUser();
		sysUser.setTable_id(user_table_id);
		String userId = "";
		if ("2".equals(sysUser.getUserType()) || "5".equals(sysUser.getUserType())) {
			userId = request.getParameter("userId");
			if (null == userId || "".equals(userId)) {
				response.getWriter().write("-1");
				response.getWriter().close();
				response.getWriter().flush();
				return;
			}
		} else {
			userId = sysUser.getTable_id();
		}
		SysUser sysUser1 = new SysUser();
		sysUser1.setTable_id(userId);
		Map map = this.sys_TestPaperServiceImpl.getTest_PaperQuestionInfo(model, sysUser1);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_question);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}
	 
}
